package com.equipment.fkdcn.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.equipment.fkdcn.pojo.EParameter;
import com.equipment.fkdcn.pojo.EType;
import com.equipment.fkdcn.pojo.Repair;
import com.equipment.fkdcn.service.EparameterService;
import com.equipment.fkdcn.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/rep")
public class RepairController {

    @Autowired
    @Qualifier("repairService")
    private RepairService repairService;

    @Autowired
    @Qualifier("eparameterService")
    private EparameterService eparameterService;


    //查询
    @GetMapping("/repList")
    @ResponseBody
    public String repairList(@RequestParam("name") String name){
        List<Repair> repairs = repairService.allQuery();
        ArrayList<JSONObject> jsonObjects = new ArrayList<JSONObject>();
        ArrayList<JSONObject> jsonObjects2 = new ArrayList<JSONObject>();
        for (Repair r:repairs){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",r.getId());
            jsonObject.put("deliveryPlace",r.getDeliveryPlace());
            jsonObject.put("giveRepair",r.getGiveRepair());
            jsonObject.put("eName",r.getEpId().geteName());
            jsonObject.put("etype",r.getEpId().getEtype().getType());
            jsonObject.put("model",r.getEpId().getModel());
            jsonObject.put("reason",r.getReason());
            jsonObject.put("repairman",r.getRepairman());
        /*    jsonObject.put("recovery",r.getRecovery());*/
            jsonObjects.add(jsonObject);
        }
        if(name!=null&&!name.equalsIgnoreCase("")){
            for (int i=0;i<jsonObjects.size();i++){
                JSONObject jsonObject = jsonObjects.get(i);
                Set<String> keySet = jsonObject.keySet();
                Iterator<String> iterator = keySet.iterator();
                while (iterator.hasNext()){
                    String key=iterator.next().toString();
                    String temp = jsonObject.getString(key);
                    System.out.println(temp);
                    if (temp.indexOf(name)>=0){
                        jsonObjects2.add(jsonObjects.get(i));
                        break;
                    }
                }
            }
            jsonObjects=jsonObjects2;
        }
        String json = JSON.toJSONString(jsonObjects);
        return  json;
    }


    //删除功能
    @GetMapping("/del")
    @ResponseBody
    public String del(@RequestParam("param")String data){
        String newData = data.substring(1, data.length() - 1);
        String[] list = newData.split(",");
        //转换为int数组
        boolean flag=true;
        for (int i=0;i<list.length;i++){
            boolean b = repairService.del(Integer.parseInt(list[i]));
            if (!b){
                flag=false;
                break;
            }
        }
        return  "{\"state\":"+flag+"}";
    }


    //查询设备
    /*@PostMapping("/queryEp")
    @ResponseBody
    public String queryName(@RequestBody String data){
        System.out.println(data);
        JSONObject jsonObject = JSON.parseObject(data);
        EParameter eParameter = JSON.toJavaObject(jsonObject, EParameter.class);
        List<EParameter> list = eparameterService.vagueQuery(eParameter);
        boolean flag=false;
        int eid=0;
        for (int i=0;i<list.size();i++){
            EParameter temp = list.get(i);
            int id = temp.getEtype().getId();
            if (id==eParameter.getEtype().getId()){
                flag=true;
                eid=temp.getEid();
                break;
            }else{
                eid=0;
            }
        }
        boolean f=false;
        if (list.size()>0&&flag) f=true;
        return  "{\"state\":"+f+",\"eId\":"+eid+"}";
    }

    */

    @PostMapping("/queryEp")
    @ResponseBody
    public String queryName(@RequestBody  String data){
        System.out.println(data);
        JSONObject jsonObject = JSONObject.parseObject(data);
        String eName = jsonObject.getString("eName");
        String typeId = jsonObject.getString("typeId");
        String eModel = jsonObject.getString("eModel");

        List<EParameter> ep = eparameterService.queryName(eName,typeId);

        int ied=0;
        if (ep.size()>0) ied=ep.get(0).getEid();

        boolean f=ep.size()>0?true:false;

        if(eModel!=null){
            System.out.println(ep.get(0).getModel()+"======="+eModel);
            if (ep.get(0).getModel().equals(eModel)) return  "{\"state\":"+f+",\"eId\":"+ied+"}";

        }else  if (eModel!=null&&!ep.get(0).equals(eModel)){

            return  "{\"state\":"+f+",\"eId\":"+0+"}";//eModel不等于空,并且!ep.equals(eModel)
        }

        return  "{\"state\":"+false+",\"eId\":"+ied+"}";
    }



    //查询设备二
    @PostMapping("/queryEp2")
    @ResponseBody
    public  String iopQuery(@RequestBody  String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        String eName = jsonObject.getString("eName");
        String typeId = jsonObject.getString("typeId");
        String eModel = jsonObject.getString("eModel");
        EParameter eParameter = new EParameter();

        eParameter.setModel(eModel);
        eParameter.seteName(eName);
        EType eType = new EType();
        if (typeId!=null){
                eType.setId(Integer.parseInt(typeId));
        }else {
            eType.setId(0);
        }
        eParameter.setEtype(eType);
        List<EParameter> eParameters = eparameterService.vagueQuery(eParameter);
        return "{\"state\":"+(eParameters.size()>0)+"}";
    }



    //新增
    @PostMapping("/addRep")
    @ResponseBody
    public String addRep(@RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
        String tid = jsonObject.getString("tid");
        String receiver = jsonObject.getString("receiver");
        String giveTime = jsonObject.getString("giveTime");
        String mailPlace = jsonObject.getString("mailPlace");
        String damage = jsonObject.getString("damage");
        stringStringHashMap.put("eid",tid);
        stringStringHashMap.put("deliveryPlace",mailPlace);
        stringStringHashMap.put("repairman",receiver);
        stringStringHashMap.put("giveRepair",giveTime);
        stringStringHashMap.put("reason",damage);
        boolean b = repairService.addRepair2(stringStringHashMap);
        return  "{\"state\":"+b+"}";
    }


}
